﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using DAL;

namespace UI
{
    public partial class thanhtoan : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                MultiView1.ActiveViewIndex = 0;
                if (!IsPostBack)
                {
                    int tongtien = 0;
                    DataTable dt = (DataTable)Session["GioHang"];
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        tongtien = tongtien + int.Parse(dt.Rows[i]["dongia"].ToString()) * int.Parse(dt.Rows[i]["soluong"].ToString());
                        lbtongtien.Text = "Tổng số tiền: " + tongtien.ToString() + " VNĐ";
                    }
                }

                DataTable dt1 = DBConnection.getALL("sp_getAllPTThanhToan");
                cpptthanhtoan.DataSource = dt1;
                cpptthanhtoan.DataValueField = "MaPT";
                cpptthanhtoan.DataTextField = "TenPT";
                
                cpptthanhtoan.DataBind();
                DataTable dt2 = DBConnection.getALL("sp_getAllHinhThucGiaoHang");
                cphtgiaohang.DataSource = dt2;
                cphtgiaohang.DataValueField = "MaHT";
                cphtgiaohang.DataTextField = "TenHT";
                cphtgiaohang.DataBind();

            }
            
        }

        protected void btndathang_Click(object sender, EventArgs e)
        {
            MultiView1.ActiveViewIndex = 1;

            string tennguoinhan = txtnguoinhan.Text;
            string diachi = txtdiachi.Text;
            string sdt = txtsodt.Text;
            string tendn = Session["TenDN"].ToString();
            int MaKH = int.Parse(Session["MaKH"].ToString());
            DateTime ngaydat = DateTime.Now;
            DateTime ngaygiao;
            int MaHTGH = int.Parse(cphtgiaohang.SelectedValue.ToString());
            int MaPTTT = int.Parse(cpptthanhtoan.SelectedValue.ToString());
            bool dagiao = false;
            //int tongtien = 0;
            SqlParameter[] thamso = new SqlParameter[]
            {
              new SqlParameter("MaHT",int.Parse(cphtgiaohang.SelectedValue.ToString())),
            };

            DataTable dt6 = DBConnection.GetDatatableByStoreProcedureName("gethinhthucgiaohangbyMaHT", thamso);
            int mucphi = int.Parse(dt6.Rows[0]["MucPhi"].ToString());
            int phiGH = mucphi;
            if (mucphi == 50000)
            {
                ngaygiao = ngaydat;
            }
            else if (mucphi == 30000)
            {
                ngaygiao = ngaydat.AddDays(3);
            }
            else
                ngaygiao = ngaydat.AddDays(5);
            DataTable dt = (DataTable)Session["GioHang"];

            //for (int i = 0; i < dt.Rows.Count; i++)
            //{
            //    tongtien = tongtien + int.Parse(dt.Rows[i]["dongia"].ToString()) * int.Parse(dt.Rows[i]["soluong"].ToString());

            //}
            //tongtien += phiGH; 

            // Get Max DonHang
            DataTable dtddh = DBConnection.getALL("sp_getCountDonHang");
            int maddh = int.Parse(dtddh.Rows[0]["soluongDH"].ToString()) + 2;
            //string tinhtrang = "False";

            SqlParameter[] thamso1 = new SqlParameter[]
            {
                new SqlParameter("MaDH",maddh),
                new SqlParameter("MaKH",MaKH),
                new SqlParameter("HoTenNguoiNhan",tennguoinhan),
                new SqlParameter("DiaChi",diachi),
                new SqlParameter("SDT",sdt),
                new SqlParameter("NgayMua",ngaydat),
                new SqlParameter("NgayGiao",ngaygiao),
                new SqlParameter("MaPT_ThanhToan",MaPTTT),
                new SqlParameter("MaHT_GiaoHang",MaHTGH),
                new SqlParameter("DaGiao",dagiao),
            };

            DBConnection.GetDatatableByStoreProcedureName("insertDonHang", thamso1);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                string masp = dt.Rows[i]["maSP"].ToString();
                int soluong = int.Parse(dt.Rows[i]["soluong"].ToString());
                int gia = int.Parse(dt.Rows[i]["soluong"].ToString()) * int.Parse(dt.Rows[i]["dongia"].ToString());

                SqlParameter[] thamso2 = new SqlParameter[] { 
                    new SqlParameter("@MaSP",masp),
                    new SqlParameter("@MaDH",maddh),
                    new SqlParameter("@SoLuong",soluong),
                    new SqlParameter("@DonGia",gia)
               
                };
                DBConnection.GetDatatableByStoreProcedureName("insertChiTietDonHang", thamso2);
            }
            dt.Rows.Clear();
            Session["GioHang"] = null;
        }

        protected void cphtgiaohang_SelectedIndexChanged(object sender, EventArgs e)
        {
            SqlParameter[] thamso = new SqlParameter[]
                {
                    new SqlParameter("MaHT",int.Parse(cphtgiaohang.SelectedValue.ToString())),
                };

            DataTable dt6 = DBConnection.GetDatatableByStoreProcedureName("gethinhthucgiaohangbyMaHT", thamso);
            int mucphi = int.Parse(dt6.Rows[0]["MucPhi"].ToString());
            int tongtien = 0;
            DataTable dt = (DataTable)Session["GioHang"];
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                tongtien = tongtien + int.Parse(dt.Rows[i]["dongia"].ToString()) * int.Parse(dt.Rows[i]["soluong"].ToString());
            }
            tongtien += mucphi;
            lbtongtien.Text = tongtien.ToString(); 
        }
    }
}